package com.easyfun.healthmagicbox.alarmclock;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.easyfun.healthmagicbox.util.PostFile;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;

public  class SoundMeter {
	static final private double EMA_FILTER = 0.6;
	static final private String TAG =SoundMeter.class.getSimpleName();
	private MediaRecorder mRecorder = null;
	private double mEMA = 0.0;
	AudioSpeexProcessor audiospeex = new AudioSpeexProcessor();

	private MediaPlayer mMediaPlayer = new MediaPlayer();
	public void startRecord(final String name) {
		new Thread ()
		{
			public void run()
			{
				audiospeex.record(name);				
			}
		}.start();
		/*
		if (mRecorder == null) {
			mRecorder = new MediaRecorder();
			mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			mRecorder.setOutputFile(name);
			try {
				mRecorder.prepare();
				mRecorder.start();
				
				mEMA = 0.0;
			} catch (IllegalStateException e) {
				System.out.print(e.getMessage());
			} catch (IOException e) {
				System.out.print(e.getMessage());
			}

		}
		*/
	}

	public void stop() {
		audiospeex.stop();
//		if (mRecorder != null) {
//			mRecorder.stop();
//			mRecorder.release();
//			mRecorder = null;
//		}
	}

	/*public void pause() {
		if (mRecorder != null) {
			mRecorder.stop();
		}
	}

	public void start() {
		if (mRecorder != null) {
			mRecorder.start();
		}
	}

	public double getAmplitude() {
		if (mRecorder != null)
			return (mRecorder.getMaxAmplitude() / 2700.0);
		else
			return 0;

	}

	public double getAmplitudeEMA() {
		double amp = getAmplitude();
		mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
		return mEMA;
	}*/
	
	void playMusic(String filename)
	{
		audiospeex.play(filename);
		
		
		/*
		try {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.stop();
			}
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(name);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
			mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
	}
}
